package com.zhonglz.util.file;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Paths;

import org.apache.commons.io.IOUtils;
import org.ofdrw.converter.ConvertHelper;
import org.ofdrw.converter.FontLoader;

import com.aspose.words.Document;
import com.itextpdf.layout.element.Image;
import com.zhonglz.util.minio.MinioUtils;
import com.zhonglz.util.ofd.OFDRender;


/***
 * 相关格式转pdf
 * @author zhonglz
 * import com.aspose.words.Document;这个包找不到
 * jar包如下：
 * 项目的resources目录中有，以及百度云
 * 链接: https://pan.baidu.com/s/1riuxk-1koRSNZkK-D1G9eA  密码: iqvo
 * 自行导入到自己的私服上或者：
 * 右键项目 -> Build Path -> Configure Build Path ->Libraries ->Add External JARS 
 * 不过打包的时候需自行解决，
 * https://www.cnblogs.com/lukelook/p/8673778.html
 * 
 * @date 2021年1月29日
 */
public class FileConversionUtils {
	
	/***
	 * word转pdf
	 * @param wordFile  word文件路径
	 * @param pdfFile   pdf文件路径
	 */
	public static boolean wordToPdf(String wordFile,String pdfFile){
		boolean bool=true;
		try {
			
            //doc路径
           Document document = new Document(wordFile);
           //pdf路径
           File outputFile = new File(pdfFile);
           //操作文档保存
           document.save(outputFile.getAbsolutePath(), com.aspose.words.SaveFormat.PDF);
       } catch (Exception e) {
           e.printStackTrace();
           bool=false;
       }
		return bool;
	}
	/***
	 * word转pdf
	 * @param wordFile  word文件路径
	 * @param pdfName   pdf文件路径
	 */
	public static boolean wordToPdfEx(String wordFile,String pdfName){
		boolean bool=true;
		try {
			InputStream wordFileIn = MinioUtils.getObject(wordFile);
			
			ByteArrayOutputStream os = new ByteArrayOutputStream();
            //doc路径
           Document document = new Document(wordFileIn);
           //操作文档保存
           document.save(os, com.aspose.words.SaveFormat.PDF);
           InputStream input = new ByteArrayInputStream(os.toByteArray());
           MinioUtils.putObject(pdfName, input);
       } catch (Exception e) {
           e.printStackTrace();
           bool=false;
       }
		return bool;
	}

	
	/***
	 * pdf转ofd
	 * @param pdfFile  pdf文件路径
	 * @param ofdFile  ofd文件路径
	 */
	public static boolean pdfToOfd(String pdfFile,String ofdFile){
		boolean bool=true;
		try {
			//转换
			 OFDRender.convertPdfToOfd(new FileInputStream(pdfFile), new FileOutputStream(ofdFile));
       } catch (Exception e) {
           e.printStackTrace();
           bool=false;
       }
		return bool;
	}
	
	
	/***
	 * pdf转ofd
	 * @param pdfName  pdf存储路径
	 * @param ofdName  ofd存储路径
	 */
	public static boolean pdfToOfdEx(String pdfName,String ofdName){
		boolean bool=true;
		ByteArrayOutputStream ofdOut = new ByteArrayOutputStream();
		try {
			//转换
			InputStream pdfInput = MinioUtils.getObject(pdfName);
			 OFDRender.convertPdfToOfd(pdfInput,ofdOut);
			MinioUtils.putObject(ofdName, new ByteArrayInputStream(ofdOut.toByteArray()));
       } catch (Exception e) {
           e.printStackTrace();
           bool=false;
       }
		return bool;
	}
		/***
		 * ofd转pdf
		 * @param ofdName  ofd存储路径
		 * @param pdfName  pdf存储路径
		 */
		public static boolean ofdToPdfEx(String ofdName,String pdfName){
			boolean bool=true;
			ByteArrayOutputStream pdfOut = new ByteArrayOutputStream();
			try {
				//转换
				InputStream ofdInput = MinioUtils.getObject(ofdName);
				FontLoader.getInstance()
	            .addAliasMapping(null, "小标宋体", "方正小标宋简体", "方正小标宋简体")
	            .addAliasMapping(null, "KaiTi_GB2312", "楷体", "楷体")
	            .addSimilarFontReplaceRegexMapping(null, ".*Kai.*", null, "楷体")
	            .addSimilarFontReplaceRegexMapping(null, ".*Kai.*", null, "楷体")
	            .addSimilarFontReplaceRegexMapping(null, ".*MinionPro.*", null, "SimSun")
	            .addSimilarFontReplaceRegexMapping(null, ".*SimSun.*", null, "SimSun")
	            .addSimilarFontReplaceRegexMapping(null, ".*Song.*", null, "宋体")
	            .addSimilarFontReplaceRegexMapping(null, ".*MinionPro.*", null, "SimSun");
		        FontLoader.setSimilarFontReplace(true);
		        ConvertHelper.toPdf(ofdInput, pdfOut);
				MinioUtils.putObject(pdfName, new ByteArrayInputStream(pdfOut.toByteArray()));
			} catch (Exception e) {
				e.printStackTrace();
				bool=false;
			}
			return bool;
	}
	/***
	 * ofd转pdf
	 * @param ofdFile  ofd文件路径
	 * @param pdfFile  pdf文件路径
	 */
	public static boolean ofdTopdf(String ofdFile,String pdfFile){
		boolean bool=true;
		try {
			FontLoader.getInstance()
            .addAliasMapping(null, "小标宋体", "方正小标宋简体", "方正小标宋简体")
            .addAliasMapping(null, "KaiTi_GB2312", "楷体", "楷体")
            .addSimilarFontReplaceRegexMapping(null, ".*Kai.*", null, "楷体")
            .addSimilarFontReplaceRegexMapping(null, ".*Kai.*", null, "楷体")
            .addSimilarFontReplaceRegexMapping(null, ".*MinionPro.*", null, "SimSun")
            .addSimilarFontReplaceRegexMapping(null, ".*SimSun.*", null, "SimSun")
            .addSimilarFontReplaceRegexMapping(null, ".*Song.*", null, "宋体")
            .addSimilarFontReplaceRegexMapping(null, ".*MinionPro.*", null, "SimSun");
	        FontLoader.setSimilarFontReplace(true);
			//转换
	        ConvertHelper.toPdf(Paths.get(ofdFile), Paths.get(pdfFile));
       } catch (Exception e) {
           e.printStackTrace();
           bool=false;
       }
		return bool;
	}
	
	
	
	
	
	
	
	public static void main(String[] args) {
		//word转pdf
		String wordFile = "C:\\Users\\Administrator\\Desktop\\CRM的问题tab页下内容不同权限用户访问不同数据.docx";
		String pdfFile1 = "C:\\Users\\Administrator\\Desktop\\2.pdf";
		if(wordToPdf(wordFile,pdfFile1)){
			System.out.println("word转pdf成功");
		}else{
			System.out.println("word转pdf失败");
		}
//		String imgFile="/Users/xiaozhong/file_minio/file/测试img.png";
//		String pdfFile2="/Users/xiaozhong/file_minio/file/测试pdf结果1.pdf";
//		if(imageToPdf(imgFile,pdfFile2)){
//			System.out.println("img转pdf成功");
//		}else{
//			System.out.println("img转pdf失败");
//		}
		//pdfToOfd("D:\\gitee_java_space\\PDF2OFD\\render\\src\\test\\resources\\n.pdf","d:\\n.ofd");
		
	}
}
